﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.DomainServices.Server;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace RiaToDalLinq.DomainServices
{
    /// <summary>
    /// 
    /// </summary>
    public class LegacyDalDomainServiceBase : DomainService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryDescription"></param>
        /// <param name="validationErrors"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public override System.Collections.IEnumerable Query(QueryDescription queryDescription, out IEnumerable<ValidationResult> validationErrors, out int totalCount)
        {
            validationErrors = new List<ValidationResult>();
            totalCount = 0;
            ActiveQueryDescription = queryDescription;

            System.Collections.IEnumerable query = base.Query(queryDescription, out validationErrors, out totalCount);

            // If the total count is not set by the query that was just invoked then set it now (if we have a value)
            if (totalCount < 0 && TotalRecordCount.HasValue && TotalRecordCount.Value > -1)
            {
                totalCount = TotalRecordCount.Value;
            }

            return query;
        }

        /// <summary>
        /// 
        /// </summary>
        public LegacyDalDomainServiceBase()
        {
            System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User;
        }

        /// <summary>
        /// 
        /// </summary>
        public QueryDescription ActiveQueryDescription
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public int? TotalRecordCount
        {
            get;
            set;
        }
    }
}
